<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" class="login-left">
        <img src="~@/assets/image/logo.png" alt="" />
        <p class="login-title">动力源</p>
        <div class="login-left-center">
          <p>诊断助手管理平台</p>
        </div>
      </el-col>
      <el-col :span="12" class="login-right">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">登 录</h3>
          </div>

          <el-form-item prop="loginName">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.loginName"
              placeholder="请输入用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin"
            class="login-btn"
            >登 录</el-button
          >
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("登录密码不能少于6位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        loginName: "",
        password: "",
      },
      loginRules: {
        loginName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, message: "长度至少2个字符字符", trigger: "blur" },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then((res) => {
              if (res.code == 200) {
                this.$message({
                  message: res.message,
                  type: "success",
                });
                this.$router.push({ path: "/" });
              }
              this.loading = false;
            })
            .catch((err) => {
              console.log(err);
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 3px 5px 3px 15px;
      height: 47px;
    }
  }

  .el-form-item {
    border: 1px solid #d9d9d9;
    border-radius: 24px;
  }
}
</style>

<style lang="scss" scoped>
@import "@/styles/common.scss";

.login-left {
  height: 100vh;
  background-image: url(~@/assets/image/login-bg.svg);
  background-repeat: no-repeat;
  background-position: 100%;
  background-size: auto 100%;
  color: #ffffff;
  @include flexLayout(center, center);
  flex-direction: column;
  filter: hue-rotate(20deg);
  img {
    width: 80px;
  }
  .login-title {
    @include flexLayout(center, flex-start);

    font-size: 28px;
    margin: 10px 0;
  }
  .login-left-center {
    font-size: 40px;
    letter-spacing: 0.15em;
  }
}

.login-right {
  height: 100vh;
  @include flexLayout(center, center);
}

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 500px;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    border-radius: 12px;
    padding: 80px 50px 40px;
  }
  .login-btn {
    margin-top: 50px;
    border-radius: 24px;
  }

  .svg-container {
    padding: 3px 5px 3px 15px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      margin: 0px auto 80px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
    color: #999;
  }
  .svg-icon{
    color: #999;
  }
}
</style>
